package com.cyy.set;

import com.cyy.map.Map;
import com.cyy.map.TreeMap;

/**
 * 利用 TreeMap 来实现
 * JDK 底层 TreeSet 也是如此
 * @author 会玩的洋洋
 * @since 2022-02-27
 * @param <E>
 */
public class TreeSet<E>{
    private Map<E, Object> map = new TreeMap<>();

    public int size() {
        return map.size();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public void clear() {
        map.clear();
    }

    public boolean contains(E element) {
        return map.containsKey(element);
    }

    public void add(E element) {
        map.put(element, null);
    }

    public void remove(E element) {
        map.remove(element);
    }

    public void traversal(Map.Visitor<E, Object> visitor) {
        map.traversal(new Map.Visitor<E, Object>() {
            @Override
            public boolean visit(E key, Object value) {
                return visitor.visit(key, null);
            }
        });
    }
}
